//
//  MyView.swift
//  MySwiftUIDemo
//
//  Created by wangws1990 on 2024/11/13.
//

import SwiftUI

//小号 170*170
//中号 364 * 170
//大号 364 * 382
struct MyView: View {
    @State private var listData: [Item] = []
    @State private var listDataInfo: [ItemInfo23] = []
    @State private var currentPage: Int = 0
    @State var isLogo: Bool = false
    
    func getR(_ w: CGFloat,lanceLeft: Bool)->CGFloat{
        if lanceLeft{//12
            //18
            let width = (w - 4 * 15)/3.0/600
            return width
        }
        let width = (w - 3 * 15)/2.0/550
        return width
    }
    
    var body: some View {
        ZStack{
            Color.white
            GeometryReader { geo in
                let w = geo.size.width
                let h = geo.size.height
                ScrollView(.vertical,showsIndicators: false) {
                    LazyVStack(alignment: .leading,spacing: 15) {
                        ForEach(listDataInfo.indices,id: \.self) { index in
                            let objc = listDataInfo[index]
                            let r = getR(geo.size.width, lanceLeft: geo.size.width>geo.size.height)
                            MyViewCell(listData: objc.listData,number: geo.size.width < geo.size.height ? 12 : 18,lanceLeft: geo.size.width > geo.size.height,r: r).frame(width: geo.size.width,height: 410*r).id("\(objc.listData)")
                        }
                    }.id("\(w)-\(h)")
                }.onAppear {
                    self.listData = getData()
                    loadData(geo.size.width < geo.size.height)
                }.onChange(of: geo.size.width > geo.size.height) { oldValue, newValue in
                    loadData(geo.size.width < geo.size.height)
                }
            }
        }
    }
    
    let task = Task()
    func getData()-> [Item]{
        var list: [Item] = []
        let listData =  [Item(width: 1,height: 1),Item(width: 2,height: 1),Item(width: 1,height: 1), Item(width: 2,height: 1),Item(width: 2,height: 2),Item(width: 4,height: 2)]
        for i in 0...19{
//            let index = Int(arc4random_uniform(100))%listData.count
//            var item = listData[index]
//            item.name = "\(i + 1)"
            list.append(contentsOf: listData)
        }
        return list
    }
    // h 8 + 4
    // h 6 + 6
    // h 4 + 4 + 4
    // l 8 + 8
    // l 8 + 6 + 2
    // l 6 + 2 + 6 + 2
    // l 4 + 4 + 4 + 4
    
    //8
    //6
    //4
    
    func loadData(_ port: Bool){
        var listData: [[Item]] = []
        self.listDataInfo.removeAll()
        let number = port ? 12 : 16
        let list = self.listData
        let result = task.create2DArray(with: list, targetSum: number)
        result.forEach { objc in
            listData.append(objc)
        }
        
        listData.forEach { listNew in
            if have8(listNew){
                let li = task.create2DArray(with: listNew, targetSum: 8)
                self.listDataInfo.append(ItemInfo23(listData: li))
            }else if sum(listNew) == 12 && list.count == 4{ // 4 + 4 + 2 + 2
                let li =  task.create2DArray(with: listNew, targetSum: 4)
                self.listDataInfo.append(ItemInfo23(listData: li))
            }else{ 
                var li =  task.create2DArray(with: listNew, targetSum: 6)
                var needDecode = false
                for (_,objc) in li.enumerated(){
                    if haveDont(objc){
                        needDecode = true
                        break
                    }
                }
                if needDecode{
                    li = task.create2DArray(with: listNew, targetSum: 4)
                }
                self.listDataInfo.append(ItemInfo23(listData: li))
            }
        }
//        self.listDataInfo = Array(self.listDataInfo.prefix(3))
//        for (i,objc) in listDataInfo.enumerated(){
//            debugPrint("===",i,objc.listData,task.getTotalSum(objc.listData))
//        }
    }
    func have8(_ item: [Item])->Bool{
        let list = item.filter { objc in
            return objc.width * objc.height == 8
        }
        return list.count > 0
    }
    func sum(_ item: [Item])->Int{
        return task.getSum(item)
    }
    func haveDont(_ list: [Item]) ->Bool{
        if sum(list) == 6 && (haveData(list, sum: 2, count: 3) || haveData(list, sum: 2, count: 1) && haveData(list, sum: 4, count: 1)){
            return true
        }
        return false
    }
    func haveData(_ list: [Item], sum: Int, count: Int)->Bool{
        let li = list.filter { objc in
            return objc.width * objc.height == sum
        }
        return li.count == count
    }
}
struct MyViewCell: View {
    var listData: [[Item]]
    var number : Int
    var lanceLeft: Bool = false
    var r: CGFloat = 1.0
    let task = Task()
    var body: some View {
        GeometryReader { geo in
            let width = geo.size.width
            let height = geo.size.height
            ZStack{
                HStack(spacing: 15*r) {
                    ForEach(listData.indices,id: \.self) { index in
                        let list = listData[index]
                        let sum  = task.getSum(list)
                        let w = getWidth(list)*r
                        if sum == 8{
                            if list.count == 1{
                                getExLargetView(getItem(list, data: 8, index: 0)).frame(width: w, height: 410*r, alignment: .center)
                            }else{
                                get8View(list).frame(width: w, height: 410*r, alignment: .center)
                            }
                        }else if sum == 4{
                            get4View(list).frame(width: w, height: 410*r, alignment: .center)
                        }else if sum == 6{
                            get6View(list).frame(width: w, height: 410*r, alignment: .center)
                        }else if sum < 6{
                            get6View(list).frame(width: w, height: 410*r, alignment: .center)
                        }
                    }
                }.padding(.horizontal,15*r)
            }.frame(width: width,height: height)
        }
    }
    func get4View(_ list: [Item])->some View{
        ZStack{
            Color.clear
            if list.count == 1{
                getLargetView(list[0])
            }else if list.count == 2{
                VStack(alignment: .center, spacing: 20) {
                    getMediumView(list[0])
                    getMediumView(list[1])
                }
            }else if list.count == 3{
                VStack(alignment: .center, spacing: 20) {
                    HStack(alignment: .center, spacing: 20) {
                        getsmallView(getItem(list, data: 1, index: 0))
                        getsmallView(getItem(list, data: 1, index: 1))
                    }
                    getMediumView(getItem(list, data: 2, index: 0))
                }
            }else if list.count == 4{
                VStack(alignment: .center, spacing: 20) {
                    HStack(alignment: .center, spacing: 20) {
                        getsmallView(list[0])
                        getsmallView(list[1])
                    }
                    HStack(alignment: .center, spacing: 20) {
                        getsmallView(list[2])
                        getsmallView(list[3])
                    }
                }
            }
        }
    }
    func get6View(_ list: [Item])->some View{
        ZStack{
            let sum  = task.getSum(list)
            if sum == 6{
                if list.count == 3{
                    HStack(alignment: .center, spacing: 20) {
                        VStack(alignment: .center, spacing: 20) {
                            getsmallView(getItem(list, data: 1, index: 0))
                            getsmallView(getItem(list, data: 1, index: 1))
                        }
                        getLargetView(getItem(list, data: 4, index: 0))
                    }
                }else if list.count == 4{
                    VStack(spacing: 20) {
                        HStack(spacing: 20) {
                            getsmallView(getItem(list, data: 1, index: 0))
                            getMediumView(getItem(list, data: 2, index: 0))

                        }
                        HStack(spacing: 20) {
                            getsmallView(getItem(list, data: 1, index: 1))
                            getMediumView(getItem(list, data: 2, index: 1))
                        }
                    }
                }else if list.count == 5{
                    VStack(spacing: 20) {
                        HStack(spacing: 20) {
                            getsmallView(getItem(list, data: 1, index: 0))
                            getsmallView(getItem(list, data: 1, index: 1))
                            getsmallView(getItem(list, data: 1, index: 2))

                        }
                        HStack(spacing: 20) {
                            getsmallView(getItem(list, data: 1, index: 3))
                            getMediumView(getItem(list, data: 2, index: 0))
                        }
                    }
                }else if list.count == 6{
                    VStack(spacing: 20) {
                        HStack(spacing: 20) {
                            getsmallView(list[0])
                            getsmallView(list[1])
                            getsmallView(list[2])

                        }
                        HStack(spacing: 20) {
                            getsmallView(list[3])
                            getsmallView(list[4])
                            getsmallView(list[5])
                        }
                    }
                }
            }else if sum < 6{
                if sum == 5{
                    if list.count == 5{
                        VStack(spacing: 20) {
                            HStack{
                                getsmallView(getItem(list, data: 1, index: 0))
                                getsmallView(getItem(list, data: 1, index: 1))
                                getsmallView(getItem(list, data: 1, index: 2))
                            }
                            HStack{
                                getsmallView(getItem(list, data: 1, index: 3))
                                getsmallView(getItem(list, data: 1, index: 4))
                                Spacer()
                            }
                        }
                    }else if list.count == 4{
                        VStack(spacing: 20) {
                            HStack{
                                getsmallView(getItem(list, data: 1, index: 0))
                                getMediumView(getItem(list, data: 2, index: 0))
                            }
                            HStack{
                                getsmallView(getItem(list, data: 1, index: 1))
                                getsmallView(getItem(list, data: 1, index: 2))
                                Spacer()
                            }
                        }
                    }else if list.count == 3{
                        VStack(spacing: 20) {
                            HStack{
                                getsmallView(getItem(list, data: 1, index: 0))
                                getMediumView(getItem(list, data: 2, index: 0))
                            }
                            HStack{
                                getMediumView(getItem(list, data: 2, index: 1))
                                Spacer()
                            }
                        }
                    }else if list.count == 2{
                        HStack(alignment: .center, spacing: 20) {
                            getLargetView(getItem(list, data: 4, index: 0))
                            getsmallView(getItem(list, data: 1, index: 0))
                        }
                    }
                }else if sum == 4{
                    if list.count == 1{
                        HStack(spacing: 0) {
                            getLargetView(list[0])
                            Spacer()
                        }
                    }else if list.count == 2{
                        VStack(spacing: 20) {
                            HStack{
                                getMediumView(getItem(list, data: 2, index: 0))
                                Spacer()
                            }
                            HStack{
                                getMediumView(getItem(list, data: 2, index: 1))
                                Spacer()
                            }
                        }
                    }else if list.count == 3{
                        VStack(spacing: 20) {
                            HStack{
                                getsmallView(getItem(list, data: 1, index: 0))
                                getMediumView(getItem(list, data: 2, index: 0))
                            }
                            HStack{
                                getsmallView(getItem(list, data: 1, index: 1))
                                Spacer()
                            }
                        }
                    }else if list.count == 4{
                        VStack(spacing: 20) {
                            HStack{
                                getsmallView(getItem(list, data: 1, index: 0))
                                getsmallView(getItem(list, data: 1, index: 1))
                                getsmallView(getItem(list, data: 1, index: 2))
                            }
                            HStack{
                                getsmallView(getItem(list, data: 1, index: 3))
                                Spacer()
                            }
                        }
                    }
                }else if sum == 3{
                    if list.count == 3{
                        VStack(spacing: 20) {
                            HStack{
                                getsmallView(getItem(list, data: 1, index: 0))
                                getsmallView(getItem(list, data: 1, index: 1))
                                getsmallView(getItem(list, data: 1, index: 2))
                            }
                            Spacer()
                        }
                    }else{
                        VStack(spacing: 20) {
                            HStack{
                                getsmallView(getItem(list, data: 1, index: 0))
                                getMediumView(getItem(list, data: 2, index: 0))
                            }
                            Spacer()
                        }
                    }
                }else if sum == 2{
                    if list.count == 1{
                        VStack(spacing: 20) {
                            HStack{
                                getMediumView(getItem(list, data: 2, index: 0))
                                Spacer()
                            }
                            Spacer()
                        }
                    }else{
                        VStack(spacing: 20) {
                            HStack{
                                getsmallView(list[0])
                                getsmallView(list[1])
                                Spacer()
                            }
                            Spacer()
                        }
                    }

                }else{
                    VStack(spacing: 20) {
                        HStack{
                            getsmallView(list[0])
                            Spacer()
                        }
                        Spacer()
                    }
                }
            }
        }
    }
    func get8View(_ list: [Item])->some View{
        ZStack{
            Color.clear
            let sum = task.getSum(list)
            if sum == 8{
                if list.count == 1{
                    getExLargetView(getItem(list, data: 8, index: 0))
                }else if list.count == 2{
                    HStack(alignment: .center, spacing: 20) {
                        getLargetView(list[0])
                        getLargetView(list[1])
                    }
                }else if list.count == 3{
                    HStack(alignment: .center, spacing: 20) {
                        VStack(alignment: .center, spacing: 20) {
                            getMediumView(getItem(list, data: 2, index: 0))
                            getMediumView(getItem(list, data: 2, index: 1))
                        }
                        getLargetView(getItem(list, data: 4, index: 0))
                    }
                }
                else if list.count == 4{
                    let listNew = task.create2DArray(with: list, targetSum: 4)
                    HStack(alignment: .center, spacing: 20) {
                        if let objc1 = listNew.first{
                            get4View(objc1)
                        }
                        if let last = listNew.last{
                            get4View(last)
                        }
                    }
                }
                else if list.count > 4{
                    let listNew = task.create2DArray(with: list, targetSum: 4)
                    HStack(alignment: .center, spacing: 20) {
                        if let objc1 = listNew.first{
                            get4View(objc1)
                        }
                        if let last = listNew.last{
                            get4View(last)
                        }
                    }
                }
            }else if sum < 8{
                let listNew = task.create2DArray(with: list, targetSum: 4)
                HStack(alignment: .center, spacing: 20) {
                    if let objc1 = listNew.first{
                        get4View(objc1)
                    }
                    if let last = listNew.last{
                        get4View(last)
                    }
                }
            }
        }
    }
    func getWidth(_ list: [Item])->CGFloat{
        let sum = task.getSum(list)
        if sum == 8{
            return 752
        }else if sum <= 4{
            return 364
        }else if sum <= 6{
            return 560
        }
        return 560
    }
    func getExLargetView(_ item: Item?)->some View{
        ZStack{
            if let item{
                Color.blue.frame(width: 743*r,height: 382*r)
                Text("\(item.name)").foregroundColor(.white)
            }
        }
    }
    func getLargetView(_ item: Item?)->some View{
        ZStack{
            if let item{
                Color.red.frame(width: 364*r,height: 382*r)
                Text("\(item.name)").foregroundColor(.white)
            }
        }
    }
    func getMediumView(_ item: Item?)-> some View{
        ZStack{
            if let item{
                Color.yellow.frame(width: 364*r,height: 170*r)
                Text("\(item.name)").foregroundColor(.white)
            }
        }
    }
    func getsmallView(_ item: Item?)->some View{
        ZStack{
            if let item{
                Color.gray.frame(width: 170*r,height: 170*r)
                Text("\(item.name)").foregroundColor(.white)
            }
        }
    }
    func getItem(_ list: [Item],data: Int,index: Int)->Item?{
        let li = list.filter { item in
            return item.width * item.height == data
        }
        return li[index]
//        if li.count > index{
//            return li[index]
//        }
        
    }
}
struct MyViewItemCell: View {
    var r: CGFloat
    @State var listData: [Item] = []
    func getSum()->Int{
        var sum = 0
        listData.forEach { item in
            sum = sum + item.width * item.height
        }
        return sum
    }
    var body: some View {
        getOtherView()
    }
    @ViewBuilder
    func getOtherView()->some View{
        ZStack{
            if getSum() == 8 && listData.count == 1{
                getExLargetView(listData[0])
            }
            else if getSum() == 6{
                if listData.count == 6{
                    VStack(alignment: .center, spacing: 15) {
                        HStack(alignment: .center, spacing: 15) {
                            getsmallView(listData[0])
                            getsmallView(listData[1])
                            getsmallView(listData[2])
                        }
                        HStack(alignment: .center, spacing: 15) {
                            getsmallView(listData[3])
                            getsmallView(listData[4])
                            getsmallView(listData[5])
                        }
                    }
                }else if listData.count == 5{
                    VStack(alignment: .center, spacing: 15) {
                        HStack(alignment: .center, spacing: 15) {
                            getsmallView(getData(1, index: 0))
                            getsmallView(getData(1, index: 1))
                            getsmallView(getData(1, index: 2))
                        }
                        HStack(alignment: .center, spacing: 15) {
                            getsmallView(getData(1, index: 3))
                            getMediumView(getData(2, index: 0))
                        }
                    }
                }
                else if listData.count == 4{ // 2s + 2m
                    VStack(spacing: 15) {
                        HStack(alignment: .center, spacing: 15) {
                            getsmallView(listData[0])
                            getMediumView(listData[1])
                        }
                        HStack(alignment: .center, spacing: 15) {
                            getsmallView(listData[2])
                            getMediumView(listData[3])
                        }
                    }
                }else if listData.count == 3{
                    VStack(alignment: .center, spacing: 15) {
                        HStack(alignment: .center, spacing: 15) {
                            VStack(alignment: .center, spacing: 15) {
                                getsmallView(listData[0])
                                getsmallView(listData[1])
                            }
                            getLargetView(listData[2])
                        }
                    }
                }else if listData.count == 2{
                    ZStack{
                        Color.green
                    }
                }
            }
        }
    }
    func getExLargetView(_ item: Item)->some View{
        ZStack{
            Color.blue.frame(width: 743*r,height: 382*r)
            Text("\(item.name)").foregroundColor(.white)
        }
    }
    func getLargetView(_ item: Item?)->some View{
        ZStack{
            if let item{
                Color.red.frame(width: 364*r,height: 382*r)
                Text("\(item.name)").foregroundColor(.white)
            }
        }
    }
    func getMediumView(_ item: Item?)-> some View{
        ZStack{
            if let item{
                Color.yellow.frame(width: 364*r,height: 170*r)
                Text("\(item.name)").foregroundColor(.white)
            }
        }
    }
    func getsmallView(_ item: Item?)->some View{
        ZStack{
            if let item{
                Color.yellow.frame(width: 170*r,height: 170*r)
                Text("\(item.name)").foregroundColor(.white)
            }
        }
    }
    func getNewData(_ listData: [Item])->[Item]{
        let newData: [Item] = listData.filter { item in
            return item.width * item.height == 2
        }
        var list = listData
        list.removeAll { item in
            return item.width * item.height == 2
        }
        list.insert(contentsOf: newData, at: 0)
        return list
    }
    func getData(_ isMedium: Int,index: Int)->Item?{
        let list = listData.filter { item in
            return item.width * item.height == isMedium
        }
        if list.count > index{
            return list[index]
        }
        return nil
    }
}

